Wordpress everything after this slug / link - php

Is there a way to create the follow in Wordpress:
Hyperlink to page: www.localhost.com/products/
On this page there is a list with products
If the user click on a link the browser goes for example to:
www.localhost.com/products/book-cartoon-small
The problem is both are pages I want to split up this to productlist page "IS PAGE" and product page "IS SINGLE POST"
Is there somebody who knows how to create this with a statement?
$loop = new WP_Query(array('post_type' => 'product'));
while ( $loop->have_posts() ) : $loop->the_post();
if ( is_page('') ){
?>
Go to product
<?php
}else{// if is single post.
echo get_the_title();
echo the_content();
}
endwhile;
Maybe it is possible to create a if statement with everything after products/ to do this?

Create an archive page for your products with by setting 'has_archive' => true and creating a archive-product.php template file in your theme. Next add a single-product.php template. Do no forget to reset your permalinks after adding the archive. That should give you a start.
A turorial can be found here https://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-post-types-archive-page-in-wordpress/
And do not forget to check on the wordpress stack exchange. https://wordpress.stackexchange.com/

Related

Can I display a custom template page linked to a child page on a parent page - Wordpress

I'm creating a Wordpress template from scratch for my company's website.
I don't know much about PHP but I know my old company has succeded in doing the operation I want to do but I don't find any solution on the Internet...
I would like to display in a parent page, several child page and the template page that they are linked to. That way, I could display the template page as a block that can be re-used in several other pages.
For example I created a template named home.php that will be the parent page, and a custom template introduction.php with one of my block structure. In wordpress I have created a parent page for my homepage, and a child page with my template introduction.php linked.
I would like to display in my parent page, the child page and the template content to have the structure + the content of the child page.
In each parent page, I'll have several child that can have the same custom template.
I find this solution better than creating a full static page and giving it the ID of the pages or putting my HTML directly in a page editor, but I don't know if this solution exists.
For now I have only find solutions to loop child page into the custom template and then display the template with "get_template_part" into the home.php, but it displays all my templates even if they aren't link with a child page and shows the content twice...
Thank you if you can help me with this problem and sorry for my broken English.
So you want a 'parent page' that displays all the 'child page contents' in the 'parent page' itself like blocks
add this in your parent template file,
`<parent page content here>
<?php
$args = array(
'post_parent' => $post->ID,
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'DESC'
);
$query = new WP_Query($args);
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
$template = get_post_meta( $post->ID, '_wp_page_template', true );
$template = pathinfo($template);
$template_parts = explode('-', $template['filename']);
get_template_part( $template_parts[0], $template_parts[1] );
endwhile;
endif;
?>`
This should get the child pages to the parent page, here i will be using the template file like this 'template-introduction.php' and i will be adding the 'introduction' as the template name.
This is assuming you have different templates for the child pages.
I have't tested this, in theory this should get you what you need.

Finding out which Wordpress template is used for a page from admin page

I'm trying to retrieve the filename/path of template used on the 'Edit Page'-page in the Dashboard.
Similar to what wp-includes/template-loader.php (source) does on the front end: finding out which template to render.
Unfortunately, expressions like is_front_page() - which Wordpress' template-loader.php uses to find out if it should use get_front_page_template() - don't work correctly on the admin page. Which is to be expected because those expression use the global $wp_query object, and not the current query.
What I've tried so far:
Running a post loop inside the admin page
$args = array(
'p' => get_the_ID(),
'post_type' => 'any'
);
$query = new \WP_Query($args);
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
<?= the_title(); ?><br>
Is front page: <?= is_front_page() ? 'true' : 'false' ?>
<?php endwhile; endif; ?>
Displays:
Home
Is front page: false
Using get_post_meta
<?= get_post_meta(get_the_ID(), '_wp_page_template', true); ?>
Displays:
default
...which would be the same for front-page.php on Home and page.php on another default page, so this doesn't help me.
In short
What I'm trying to get is front-page.php when I'm editing my 'Home' page. Or custom-template.php when I'm editing some page with the custom template selected. Or about-page.php when I'm editing a page called 'About'. How to get the correct filename or path?
If your specific problem is with the home page, you could use a combination of get_page_template() and comparing the edited page's ID with get_option('page_on_front') (see WordPress option reference). There's also an option, show_on_front, which indicates whether the front page shows posts or a static page.
Maybe this helps? I don't know if there are other edge cases where a different template will be used...
Use get_page_template():
<?php echo realpath(get_page_template()); ?>
It outputs something like /foo/bar/baz/wp-content/themes/your-theme/{page-template}.php
You can choose not to use realpath() and just get the template name.
The only solution I found is this:
global $template;
echo basename($template); // front-page.php
I know it's ugly, but I just couldn't find a way to not use this global variable.

Wordpress: Show custom post type details page

I have created a custom post type named as product. After that I have created a template file to show all products on that page and write the below code :
<?php $loop = new WP_Query( array( 'post_type' => 'acme_product',
'posts_per_page' => 14 ) );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div>
<div>
<?php the_post_thumbnail('thumbnail');
?>
</div>
<div>
<?php the_title( '<h2 class="entry-title"><a href="'.get_permalink().'" title="'.
the_title_attribute( 'echo=0' ).
'" rel="bookmark">', '</a></h2>' );
?>
</div>
</div>
<?php endwhile;
?>
But when I click on the product title link it will not show my product details page. Can anyone help me?
You need to save your template as single-{post_type}.php so in your case it would be single-acme_product.php and make sure that permalink is enable if not than
Login to your WordPress admin.
Go to Settings -> Permalinks. And under Common Settings, let’s use Post name.
Then click ‘Save Changes’
as per your code you don't need to define the post_type here. just use with simple code
<?php while ( have_posts() ) : the_post(); ?>
It sounds as though you've already figured out how to loop through your product type. If you're new to custom post types and are not already aware of it, I would suggest investigating the "archive" template for custom post types (archive-{post_type}.php). I saw another answer that referenced single-{post_type}.php as well; both of these can be identified in the Template Hierarchy documentation in the WP Codex. By default, if you don't provide a customized single template for your post type, it will fall back to single.php (which may require customization if you want it to perform double-duty for multiple post types).
With that in mind, and assuming you're already using single-product.php in your theme (or have otherwise coaxed Wordpress into using your template), it would be helpful to know the symptoms you're experiencing:
Are you getting a 404? This could be a permalink issue, and if you haven't already attempted it you might consider flushing your rewrite rules. This can be accomplished by simply visiting the Settings -> Permalinks panel in the Dashboard.
Is your template being used? Consider adding some HTML comments to your template, and view source on the rendered version to ensure that you're even using the correct file. Failure to load the appropriate template could be due to a filename typo or some other logic that interferes with the normal template selection logic.
If you're getting the correct template, but you're not able to display the correct details, are you doing anything particularly wonky with your single template's loop?
Without clear details on what symptoms you're actually experiencing, it's hard to give you better direction. Hopefully some of this information points you to the correct answer, but if not please share additional details as to what is happening on your end.

How to add a css styled html-list to wordpress, manageable in the dashboard

I would like to add a feature to a WordPress page to display a short list of people, like the one seen here - http://www.blenderbox.com/company/team
I have done this on other sites by styling lists with css. This time however, it's for a client and I can't trust them to copy and paste a <li>, editing the name, job title and img name without messing it up.
What is the best way to go about creating this so that it can be easily reduced/added to in the WordPress dashboard? Basically, so that the user simply clicks "add person" then fills in some fields before updating the page.
I'm new to WordPress (though I understand how it works) but I am competent coding so hopefully an informative nudge in the right direction would be sufficient.
Thanks, Ben.
Use categories. Create a new category called Person or Team or something to that effect. Then use the Post Title for their Name - any other information can be put in the post and then add the image to the Featured Image.
Create a new template for this page by copying over another template file and giving it a header like this to name it.
<?
/*
Template Name: About Us / Team / Whatever
*/
?>
And in the page call only the information you want with something like this:
//Set up the Loop
<?php
global $post;
$args = array (
'category' => 4,
'order' => 'ASC');
$teamposts = get_posts( $args );
foreach( $teamposts as $post ) : setup_postdata($post); ?>
//Call the information you want - put them in <li> if you need
<?php the_title();?>
<?php the_excerpt();?> //OR the_content
<?php the_post_thumbnail('new-image-size');?>
//Close the loop
<?php endforeach; ?>
You can then go on to call anything else you want or run another loop on the same page to call other information.
To get the right sized thumbnail you have called 'new-image-size' - to set this up add a line like this to your functions.php file.
// Post Thumbnails
add_theme_support( 'post-thumbnails' );
// name of the thumbnail, width, height, crop mode
add_image_size( 'front-page-services', 450, 270, true );

Calling a WordPress Post Based on Popularity

If you look at my site, I have an area named Top Winter Projects in the right-side navigation. I would like to automate this part of the site as I currently have to manually paste HTML into that particular section. What I want to be able to to do is have those images (which should link to the corresponding post) change based on date.
I've tried using the featured image plugin, but that doesn't seem to work with my theme for some reason. Is there an alternate route?
I've tried piecing some code together, but i'm a bit clueless when it comes to php. I'm also not sure how i'd get a picture to show up. Here is what I have, where 40, 50, & 60 are three posts. Can echo calls show pictures?
<? php
$args = array('include' => 40, 50, 60 )
echo get_posts($args);
?>
Here is my website: http://www.merrimentdesign.com
Thanks, any help is appreciated.
One way to do it is to create a loop using WP_Query and then you can put whatever content you want in the loop. I typically do this and use categories to control what posts are shown in each area. So for example if you created a category called featured, you could tag every post you want to show in that section with that category. Then you could create a simple loop like :
<?php $the_query = new WP_Query('category_name=featured&showposts=10'); ?>
<?php while ($the_query->have_posts()) : $the_query->the_post();?>
<a href="<?php the_permalink() ?>" rel="bookmark" class="postTitleLink">
<?php the_post_thumbnail(); ?>
</a>
<?php endwhile; ?>
This is assuming you have the thumbnail function enabled in your themes functions.php.
add_theme_support( 'post-thumbnails' );

Categories