Wordpress is_page_template query - wordpress

i use a Plug-in on my wordpress site which uses a query to show search results, thats the code of the query:
if ( $query->have_posts() )
{
?>
<?php echo $query->found_posts; ?> Ergebnisse gefunden<br />
<?php
while ($query->have_posts())
{
$query->the_post();
?>
<div>
<?php if ( has_post_thumbnail() ) { echo '<p>'; the_post_thumbnail("small"); echo '</p>'; } ?>
<h2><?php the_title(); ?></h2>
</div>
<?php
}
?>
Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br />
Previous | Next
<?php
}
else
{
echo "No Results Found";
}
The search should only display results from pages who uses a specific page template (mytemplate.php)
I found the Docs http://codex.wordpress.org/Function_Reference/is_page_template who explain this, but i dont get it to work inside the above query.
any help would be nice :) thx

Please do as below.
if ( is_page_template( '{enter the name of page template with extension}' ) ) {
// Enter your if code here
} else {
// Enter your else code here.
}
For this code to work you must select the page template option while creating the page in WordPress Admin Interface.

Related

Remove Page Header from Blog Posts

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

Relevanssi - Duplicate Searches logged

I'm using (and loving) Relevanssi plugin for WordPress. Having a problem though with searches being logged twice in user searches.
Found this comment in the plugin comments about it being a problem with the template.
When digging around my search template, I just removed everything, then added things piecemeal until I had double results again.
This is the line in my search template that seems to be the trouble maker, but I don't necessarily see why.
<div style="background-image: url();"></div>
It makes 0 sense to me. If that bit of STATIC html is in the content-search.php template, I get double searches logged. If I remove it, or if all of the articles have get_the_post_thumbnail_urls, then I only get one search logged.
It seems to have something to do with style="background-image:url();" not having a value?
Here's the bigger picture:
search.php:
<?php
global $wp_query;
if ( $wp_query->found_posts > 12 ) {
$search_count = '1 - ' . $wp_query->post_count . ' of ' . $wp_query->found_posts;
} else {
$search_count = $wp_query->post_count;
}
?>
<div class="search-pg-header">
<?php include get_template_directory() . '/templates/partials/search-form.php'; ?>
<?php if ( get_search_query() !== '' ) { ?>
<h1>Showing <?php echo $search_count; ?> results for <?php echo get_search_query() ?></h1>
<?php } // end if ?>
</div>
<div class="posts-container">
<?php if ( have_posts() ):
while ( have_posts() ) : the_post();
get_template_part( 'templates/content', 'search' );
endwhile;
else: ?>
<p>There are no articles that matched your search.</p>
<?php endif; ?>
</div>
<?php if ( $wp_query->max_num_pages > 1 ) { ?>
<button>Show More</button>
<?php } // end if ?>
content-search.php:
<article data-link="<?php the_permalink(); ?>">
<div style="background-image: url(<?php the_post_thumbnail_url('card-white');?>);"></div>
<div>
<h2><?php the_title(); ?></h2>
<?php if (get_post_type() === 'post') { get_template_part('templates/entry-meta'); } ?>
<?php if ( has_excerpt() ) {?>
<p><?php echo get_the_excerpt(); ?></p>
<?php } //end if ?>
</div>
</article>
I did a fresh install of Wordpress and tested out this bizarre theory with 2017 theme. True enough, if I add <div style="background-image:url();"></div> to the template template-parts/post/content-excerpt.php then duplicate searches are logged.
If that div has an image: <div style="background-image:url(http://image.com);"></div> only one search is logged.
As Mikko Saari (Relevanssi) so kindly helped me with, this is actually unintended behavior from the browser.
It's definitely nice to at least have an explanation for this.
I was able to prevent this by just putting a check to see if there was a bkgd image before I rendered the inline style to the page, and now I know to be much more careful with this kind of thing in the future!
$style = ! empty( get_the_post_thumbnail_url($the_post, 'card-white') ) ?
'style="background-image: url(' . get_the_post_thumbnail_url($the_post, 'card-white') . ');"' : '';
<div class="article-card__img" <?php echo $style; ?>></div>

Show [post_title] in content of Wordpress post?

Just wanted to show the title of a Wordpress post. I've tried with no success. Pretty new to this.
You can try this, I hope this will help you.
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
and if you want to add custom post type, in that case you need to add WP_Query(); function to get the data like:
<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
for displaying post title in specific pages either you can write:
<?php get_the_title($post_id);?>
into that page's template file or you can make a custom file in templates folder and add this code:
<?php
if (is_page( 'Page Title' ) ):
get_the_title($post_id);
endif;
?>
and include this to all of your pages.. you can check for multiple pages by using OR condition in if statement.

If Data not exists - show (custom text)

I use wordpress CMS and CMB2 METABOXES and Custom fields. Its works perfect but when I'm making loop If data in metabox not exists , this script anyway rendering empty group field. I want to give the command to script, if it is empty ----> show my custom markup code. I have this
<?php $our_advantages_items = get_post_meta( get_the_id(), 'our_advantages_block_box', true );
if( !empty( $our_advantages_items ) ) {
foreach( $our_advantages_items as $our_advantages_item ) { ?>
<div class="cey96zggvcich3">
<div class="cey96zggvcich3_cvz">
<h2><?php echo $our_advantages_item['our_advantages_block_heading']; ?></h2>
<div class="io2ji39349959jeij">
<?php echo wpautop( $our_advantages_item['our_advantages_block_content'], 1 ); ?>
</div>
</div>
</div>
<?php }
} ?>
Will be happy for any help !
Not sure I understand what you're asking, but just add an else to your if statement
<?php if( !empty( $our_advantages_items ) ) : ?>
//If not empty write your html here
<?php else: ?>
//If empty write your html here
<?php endif; ?>

wp_get_attachment_url not working

I have a template file which shows the posts of a category called downloads. for each posts i have attached a pdf file. I have given a link to download the pdf file on the page. But when i click the download link it goes to the post page and from there i have to click to download the file. Is there any way to directly download without going to the post. ? I have tried using wp_get_attachment_url as the hyper-reference.but it is not working.The code that i have used is below:
<?php /*
Template Name: Downloads Template
*/
?>
<?php get_header(); ?>
<?php
$recent = new WP_Query("cat=7&orderby=title&order=ASC");
while($recent->have_posts()):$recent->the_post();
$desc_values = get_post_custom_values("description");
?>
<div id="download_featured_image" class="<?php the_ID(); ?> download_image_title_desc">
<a href="<?php the_permalink() ?>" rel="title">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail();
}
?></a>
<a href = "" > <?php if ( is_user_logged_in() ) {
echo "Download";
}?></a>
<a href=" http://localhost/wordpress/login.php"> <?php if( !(is_user_logged_in()) )
{
echo "Please signup/login to download this file";
}
?>
</a>
<div id="Download_post_description">
<?php
if( is_array( $desc_values ) )
{
foreach($desc_values as $key => $value );
echo "$value</n>";
}
?>
</div>
</div>
<?php endwhile ?>
<?php get_footer(); ?>
I want to give the link to the uploaded pdf in the href which i have left blank. Can someone help me?
Add the PDF attachment ID as a custom field value, for example attached_pdf_id.
Get the URL using wp_get_attachment_url()
-
<?php
if ( is_user_logged_in() ) {
$pdf_link = wp_get_attachment_url( get_post_meta( get_the_ID(), 'attached_pdf_id', true ) );
if ( $pdf_link ) {
?><a href = "<?php echo $pdf_link ?>" >Download</a><?php
} else {
?>Sorry, no link available. Please contact the webmaser.<?php
}
}
?>

Resources