最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

attachments - How do I add html content to custom post type Posts using php?

programmeradmin2浏览0评论

I'm trying to do something that I'm sure is very basic, but I can not figure out how to do it.

Here's the setup: I used the CPT UI plugin to create a custom post type called board_docs, and I used the Custom Fields plugin to create a custom field for the board_docs post type. The custom field allows you to add a file as an attachment. Once this file is added, a new permalink (post) is created. When I click on that permalink, the page is empty and only has a header and footer. How would I add an iframe (and other html content) to that page using php?

I tried using the loop, have_posts, in the functions.php file, but nothing is displaying. My code looks like this:

if ( have_posts() ) :
  while ( have_posts() ) : the_post();
    echo "<script>console.log('from functions.php!!!');</script>";

    ob_start(); ?>
    <div class="my-pdf-viewer">
      <p>[iframe here]</p>
    </div>
    <?php ob_get_clean();
  endwhile;
endif;

I tried using is_single, but still nothing:

if ( is_single(883) ) {// have_posts etc...}

The console.logs work and I see the logs printing to console for the specific posts, but that's it. I can't figure out how to display the html content.

I currently have a page called Board Docs where I display the permalinks for each post. When the user clicks on each link, it takes them to that empty page where I want to display html content. How would I do that using php?

UPDATE I found this handy resource a couple days ago and just wanted to share it with others who might be struggling with finding the right hook.

Also, hooks. I wish someone could've explained to me what these were when I first started. If you're doing any custom WordPress development, they're important to know. Happy coding.

I'm trying to do something that I'm sure is very basic, but I can not figure out how to do it.

Here's the setup: I used the CPT UI plugin to create a custom post type called board_docs, and I used the Custom Fields plugin to create a custom field for the board_docs post type. The custom field allows you to add a file as an attachment. Once this file is added, a new permalink (post) is created. When I click on that permalink, the page is empty and only has a header and footer. How would I add an iframe (and other html content) to that page using php?

I tried using the loop, have_posts, in the functions.php file, but nothing is displaying. My code looks like this:

if ( have_posts() ) :
  while ( have_posts() ) : the_post();
    echo "<script>console.log('from functions.php!!!');</script>";

    ob_start(); ?>
    <div class="my-pdf-viewer">
      <p>[iframe here]</p>
    </div>
    <?php ob_get_clean();
  endwhile;
endif;

I tried using is_single, but still nothing:

if ( is_single(883) ) {// have_posts etc...}

The console.logs work and I see the logs printing to console for the specific posts, but that's it. I can't figure out how to display the html content.

I currently have a page called Board Docs where I display the permalinks for each post. When the user clicks on each link, it takes them to that empty page where I want to display html content. How would I do that using php?

UPDATE I found this handy resource a couple days ago and just wanted to share it with others who might be struggling with finding the right hook. https://adambrown.info/p/wp_hooks/hook

Also, hooks. I wish someone could've explained to me what these were when I first started. If you're doing any custom WordPress development, they're important to know. Happy coding.

Share Improve this question edited Jun 24, 2020 at 1:23 sansae asked Apr 30, 2020 at 23:03 sansaesansae 1892 silver badges10 bronze badges 3
  • What are you using ob_start() for? – Jacob Peattie Commented May 1, 2020 at 0:33
  • Do you have the ability to add 'template-parts' to your theme/child theme? – Tony Djukic Commented May 1, 2020 at 2:56
  • @TonyDjukic, yes I should be able to, although I'm not sure how to do that? Is this where I create a template-parts.php file, then "hook" it somewhere? I'm working on a child theme. The parent is Hemingway Rewritten. – sansae Commented May 1, 2020 at 20:22
Add a comment  | 

1 Answer 1

Reset to default 1

After searching through the wordpress docs, I found what I needed. What I was missing was the hook called "the_content". My function, and all the code I wrote inside it, was correct. I just needed this stupid hook.

function displayIframe($content) {
  //[my code to get the $currentPostID and $currentPostGuid]

  //...
  //...

  if (is_single( $currentPostID )) {// if on the specific custom post page
    $content .= "<iframe src='" . esc_html($currentPostGuid)  . "' width=100% height=900></iframe>";
  }

  return $content;
}

function main() {
  // the_content hook was what I needed; I had no idea it existed
  add_filter("the_content", __NAMESPACE__ . "\displayIframe");
}

I hope this is helpful for others. Goodluck :)

发布评论

评论列表(0)

  1. 暂无评论