I have a question about Wordpress' template structure and querying posts.
I have my templates setup where the (for example) archive-$posttype.php is built like:
get_header();
$args = 'page_id=18'; //Client Uses page
query_posts($args); the_post();
get_template_part( 'sub-header' );
// Reset Query
wp_reset_query();
?>
<div class="content">
<?php get_template_part( 'loop' ); ?>...
I do this to set my default $post variable for my sub-header.php file which prints out content from that page:
<div id="subheader">
<h1><?php echo get_post_meta($post->ID, 'header_title', true)?></h1>
<?php echo get_post_meta($post->ID, 'header_description', true)?>...
However, using this method on the home.php template, doesn't work:
get_header();
$temp_query = $wp_query;
$page_id = 119; //Client Uses page
#$post = get_page( $page_id );
$args = array('page_id' => $page_id);
$post = new WP_Query( $args ); the_post();
get_template_part( 'sub-header' );
wp_reset_postdata();
?>
<div class="content">
<?php get_template_part( 'loop' ); ?>
<?php get_sidebar( 'news' ); ?>
</div><!--.content -->
<?php get_footer(); ?>
I'm curious why this works on one template and not on the home template. AND, am I going about this the wrong way? What's the correct way to have page content in the sub-header template that in most cases is ACTUALLY related to that current page the user is on.
Thanks!
Not sure I understand your problem exactly but if you are doing what I think, having a chunk of text from a specific page above another loop that pulls in posts of some sort or another I would use the template naming structure.
<?php /* Template Name: My Template */ ?>
This will allow you to use the standard loop to get content from whatever page the clients sets the template to (avoiding the static ids you are using).
Your home page bug could be due to it not being set as the posts page in the reading settings. As I understand it if a page is not set as the posts page it will act like a normal page unless you rewrite the query specifically.
"What's the correct way to have page content in the sub-header template that in most cases is ACTUALLY related to that current page the user is on."
Rather than trying to tie together two unrelated pages/posts, it might be easier to use a custom field tool. My current favorite is Advanced Custom Fields.
With ACF, you can add supplemental fields (image, wysiwyg, file upload, 14 total field types) to your posts and pages, then easily pull the custom data into your template. It's very well documented and extremely easy to use.
So, I changed the way my sub-header.php template works. Basically some basic checking on what type of page/post was being set/called then dynamically pulled the relevant page info.
<?php
if (is_page()) :
$header_title = get_post_meta($post->ID, 'header_title', true);
$video_id = get_post_meta($post->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($post->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($post->ID, 'header_description', true);
elseif (is_home()) :
$page_id = 119; // News page
$page = get_page( $page_id );
$header_title = get_post_meta($page->ID, 'header_title', true);
$video_id = get_post_meta($page->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($page->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($page->ID, 'header_description', true);
elseif (is_archive()) :
$page_id = 18; // Client Uses page
$page = get_page( $page_id );
$header_title = get_post_meta($page->ID, 'header_title', true);
$video_id = get_post_meta($page->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($page->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($page->ID, 'header_description', true);
endif;
?>
<div id="subheader">
<h1><?php echo $header_title; ?></h1>
<?php if ( $video_id ) : ?>
<iframe class="visual-element" width="300" height="200" src="http://www.youtube.com/embed/<?php echo $video_id;?>?rel=0" frameborder="0" allowfullscreen></iframe>
<?php elseif ($thumbnail) : ?>
<?php echo $thumbnail; ?>
<?php endif; ?>
<?php echo $description; ?>
</div><!-- #subheader -->
Related
I'm trying to make a row layout where people can add an mp4 with ACF. So far so good, but when I try to add multiple video sections in the same post it outputs the same video in every player, even though they are different in the backend.
Does anyone know what I'm doing wrong?
Row layout:
<?php if (get_row_layout() == 'video') : ?>
<?php get_template_part('template-parts/sections/section', 'video'); ?>
<?php endif; ?>
Video section part
<div class="section section-type-video flex">
<?php
$video_mp4 = get_field('video_file'); // MP4 Field Name
$video_poster = get_field('video_poster_image'); // Poster Image Field Name
// Build the Shortcode
$attr = array(
'mp4' => $video_mp4,
'poster' => $video_poster,
'preload' => 'auto'
);
echo wp_video_shortcode($attr);
?>
</div>
Many thanks in advance!
now i have made a Flexible Content field called "video-section"
and created a layout called "video-main' which contains a URL field called
"video-url"
<?php if( have_rows('video-section') ): ?>
<?php while( have_rows('video-section') ): the_row(); ?>
<?PHP
//get the URL
if( get_row_layout() == 'video-main' ): ?>
<?php echo the_sub_field('video-url'); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
I used this code to echo all different URLs that included in the post that I was created for testing try it if that what you want to do
To answer my own question, I was targetting the wrong fields.
Needed to target sub_fields instead of get_field.
In ACF I set the return value of my subfield to array instead of url, to finish it I passed the url variable in the video shortcode array as an attribute.
Correct code below:
<div class="section section-type-video flex">
<?php
$video_mp4 = get_sub_field('video_file'); // MP4 Field Name
$video_poster = get_sub_field('video_poster_image'); // Poster Image Field Name
$video_url = $video_mp4['url'];
// Build the Shortcode
$attr = array(
'mp4' => $video_mp4,
'src' => $video_url,
'poster' => $video_poster,
'preload' => 'auto'
);
echo wp_video_shortcode($attr); ?>
</div>
please check the ACF Documentation for get_row_layout() how to display it
Click here! to see it
I have a page-header that is getting copied over to all of my blog posts that I want to remove completely. I am removing it visually via CSS, but SEO crawlers are still picking it up via the tag.
I am using a standard wordpress them augmented with Elementor. Here is a screenshot of the SEO report.
And here is a screenshot of the actual HTML code
Let me know if any of you have any additional questions! Thank you for your help!
You can make a conditional statement that just outputs the title of the post if on single blog posts, so something like this
<div class="container clr page-header-inner">
<?php
// Return if page header is disabled
if ( oceanwp_has_page_header_heading() ) { ?>
<!-- check to see if single blog posts -->
<?php if (is_single()) : ?>
<h1><?php echo get_the_title(); ?></h1>
<!-- otherwise show the existing title format -->
<?php else: ?>
<<?php echo esc_attr( $heading ); ?> class="page-header-title clr"<?php oceanwp_schema_markup( 'headline' ); ?>><?php echo wp_kses_post( oceanwp_title() ); ?></<?php echo esc_attr( $heading ); ?>>
<?php endif; ?>
<?php get_template_part( 'partials/page-header-subheading' ); ?>
<?php } ?>
<?php if ( function_exists( 'oceanwp_breadcrumb_trail' ) ) {
oceanwp_breadcrumb_trail();
} ?>
</div><!-- .page-header-inner -->
you would have to replace the existing code
I defined custome post type (Slug = portfolio).
After a Portfolio post publishes and I click on View post, page not found displays. This is my first problem.
I want when user clicks on a portfolio item it opens in an another page with more details of information. This is my another problem.
This is the code of template page for showing portfolio posts (the name of the page is: portfolio.php)
<?php
/* Template Name: Portfolio */
$arg = array('post_type'=>'portfolio');
$loop = new WP_Query($arg);
while($loop->have_posts()):
$loop->the_post();
?>
show post
<br>
<?php
endWhile;
?>
and i have single-portfolio.php with these codes:
<?php
$arg = array('post_type'=>'portfolio');
$loop = new WP_Query($arg);
while($loop->have_posts()):
$loop->the_post();
?>
show post
<br>
<?php
endWhile;
?>
please help me
The contents of your single-portfolio.php code could be something like this.
<?php
while ( have_posts() ) : the_post();
the_title();
the_content();
endwhile; // End of the loop.
?>
Okay, I have the code pulling the custom fields URL and title of the url. Now I can't seem to get it to show the second featured blog. Here is the working code.
<?php $related = get_post_meta($post->ID, "Featured-Blog", $single=true);
$related=explode(',',$related);
$args = array_merge( array('post__in' => $related, $wp_query->query ) );
query_posts($args);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="<?php the_ID(); ?>">
<p class="caption"><?php the_title(); ?></p>
</div>
<?php endwhile; else: ?>
<p>no related</p>
<?php endif; wp_reset_query();?>
This code example here produces two results, which is almost what I want. Which is caused by the foreach I believe. I do not want to use the code below, but I need to find a way to add the foreach I think to get it to list all of the featured-blogs if I have more than one.
<?php
$custom_fields = get_post_custom($post_id); //Current post id
$my_custom_field = $custom_fields['Featured-Blog']; //key name
foreach ( $my_custom_field as $key => $url )
echo $key ="<a href='".$url."'>TEST</a><br /><br /><br/>";
?>
Here is a screenshot showing my Custom Fields if it helps at all, and the results they are showing on the site. screenshot
Example 1: You are using the actual word 'TITLE' for the link use <?php the_title() ?> instead
Example 2: You are not building link at all. Your href attribute is empty. Cut echo $featured_blog1 and paste it to href attribute to end up like so:
Example 3: Same as 2
Also you can delete dose instructions or put them inside <?php ?> code so they are not visible to viewers.
Hope this helps.
If you need more info just ask. ;)
First I've created a home.php page to replace index.php and can add some custom fields on this new one and to have in it lastest 3 posts.
On home.php page I put:
<?php echo get_post_meta($post->ID, 'test', true); ?>"/>
but it doesn't works cause it tries get the post id and not the id of the page. If i put 18 (id of the page) directly it works, but i want it dinamicaly:
<?php echo get_post_meta(18, 'test', true); ?>"/>
And this condition is not satisfied for test:
if($post->post_type == 'page'){
echo 'This item is a page and the ID is: '.$post->ID;
}
Heyo,
I think your problem might be that you should have that within a loop.
For example if you were displaying several post from a category and displaying each custom field it might look like this:
<?php query_posts('cat=25&showposts=3'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li class="homeblock" style="max-width:400px;">
<span class="artist-name">
<?php $key='artist'; echo get_post_meta($post->ID, $key, true); ?>
</span>
<div class="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
</li>
<?php endwhile; endif; ?>
Otherwise (not sure how your theme is set up), what you might have to do is edit content.php and add:
<?php $key='artist'; echo get_post_meta($post->ID, $key, true); ?>
Try:
if ($post->post_type == 'page') {
<?php echo get_post_meta($page_id, 'test', true); ?>
}
Hope this helps :)